Imports Q7P1TTS.Lib

Public Class ChangePasswordForm

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

    End Sub

    Private Sub btnSubmitChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmitChange.Click
        If validateData() Then
            Dim acc = SessionHelper.Sessions("USER_INFO")
            If acc Is Nothing Then
                If MessageBox.Show("There was an unknown error found when retrieving data. System will exit.") = Windows.Forms.DialogResult.OK Then
                    Application.Exit()
                End If
            ElseIf TypeOf acc Is Employee Then
                Dim emp As Employee = acc
                emp.Password = ConvertHelper.ToMD5(txtNewPwd.Text.Trim())
                EmployeeController.save(emp)
                MessageBox.Show("Your password has been succesfully changed.")
                Me.Close()
            ElseIf TypeOf acc Is Customer Then
                Dim cust As Customer = acc
                cust.Password = ConvertHelper.ToMD5(txtNewPwd.Text.Trim())
                CustomerController.save(cust)
                MessageBox.Show("Your password has been succesfully changed.")
                Me.Close()
            End If
        End If
    End Sub

    Private Function validateData() As Boolean
        Dim res As Boolean = True
        Dim acc = SessionHelper.Sessions("USER_INFO")
        If acc Is Nothing Then
            If MessageBox.Show("There was an unknown error found when retrieving data. System will exit.") = Windows.Forms.DialogResult.OK Then
                Application.Exit()
            End If
        ElseIf TypeOf acc Is Employee Then
            Dim emp As Employee = acc
            If emp.Password <> ConvertHelper.ToMD5(txtCurrentPwd.Text.Trim()) Then
                ErrorProvider1.SetError(txtCurrentPwd, "Password is not valid. Please try again.")
                res = res And False
            Else
                ErrorProvider1.SetError(txtCurrentPwd, "")
            End If
        ElseIf TypeOf acc Is Customer Then
            Dim cust As Customer = acc
            If cust.Password <> ConvertHelper.ToMD5(txtCurrentPwd.Text.Trim()) Then
                ErrorProvider1.SetError(txtCurrentPwd, "Password is not valid. Please try again.")
                res = res And False
            Else
                ErrorProvider1.SetError(txtCurrentPwd, "")
            End If
        End If
        Return res
    End Function
End Class
